<?php

namespace app\utils\alarms;

use Carbon\Carbon;
use Guanguans\Notify\Factory;
use Throwable;

class FeiShuFactory implements AlarmsInterface
{
    /**
     * @param Throwable $throwable
     * @return void
     */
    public function push(Throwable $throwable): void
    {
        $request = request();
        $envName = match (getenv('env')){
            'local' => '本地',
            'testing' => '测试',
            'production' => '线上',
            default  => '未知'
        };
        $post = [
            'zh_cn' => [
                'title'   => "【{$envName}】" . $throwable->getMessage(),
                'content' => [
                    [
                        [
                            "tag"  => "text",
                            "text" => date("H:i:s") . " 系统异常  \n"
                        ],
                        [
                            "tag"  => "text",
                            "text" => "message： {$throwable->getMessage()} \n"
                        ],
                        [
                            "tag"  => "text",
                            "text" => "path： {$request->path()} \n"
                        ],
                        [
                            "tag"  => "text",
                            "text" => "file： {$throwable->getFile()} \n"
                        ],
                        [
                            "tag"  => "text",
                            "text" => "line： {$throwable->getLine()} \n"
                        ],
                    ]
                ]
            ]
        ];
        if(config('app.env') == 'production'){
            $post['zh_cn']['content'][0][] = [
                "tag"  => "text",
                "text" => "\n"
            ];
            $post['zh_cn']['content'][0][] = [
                "tag"  => "at",
                "user_id" => "all",
                "user_name" => "所有人",
            ];
        }
        Factory::feiShu()
            ->setToken(config('alarms.feiShu.token'))
            ->setSecret(config('alarms.feiShu.secret'))
            ->setMessage(new \Guanguans\Notify\Messages\FeiShu\PostMessage($post))
            ->send();
    }

    /**
     * @param bool $isSuccess
     * @param array $info
     * @param string $title
     * @return void
     */
    public function pushInfo(bool $isSuccess, array $info,string $title): void
    {
        if (env('ALARMS_FEI_SHU_INFO_OPEN', 'open') == 'open') {
            $envName = match (config('app.env')) {
                'local' => '本地',
                'test' => '测试',
                'production' => '线上',
                default => '未知'
            };
            $content = "";
            foreach ($info as $val) {
                $successText = $val['isSuccess'] ? '成功' : '失败';
                $content .= "[{$successText}]{{$val['time']}}：" . $val['info'] . "\n";
            }
            $elements = [
                [
                    'tag' => 'markdown',
                    'content' => $content ?? '卡片内容'
                ]
            ];
            $header = [
                'title' => [
                    'tag' => 'plain_text',
                    'content' => $title
                ],
                'subtitle' => [
                    'tag' => 'plain_text',
                    'content' => $envName . Carbon::now()
                ],
                'text_tag_list' => [
                    [
                        "tag" => "text_tag",
                        "text" => [
                            "tag" => "plain_text",
                            "content" => $isSuccess ? '成功' : '失败'
                        ],
                        "color" => $isSuccess ? 'green' : 'red'
                    ]
                ],
                'template' => $isSuccess ? 'green' : 'red'
            ];
            Factory::feiShu()
                ->setToken(config('alarms.feiShu.token'))
                ->setSecret(config('alarms.feiShu.secret'))
                ->setMessage(new \Guanguans\Notify\Messages\FeiShu\CardMessage([
                    'header' => $header,
                    'elements' => $elements
                ]))
                ->send();
        }
    }
}
